<?php

/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();
$district = null;

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["citys"] = array();
    $i = 0;
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $city = array();
        $city["cid"] = $row["cid"];
        $city["name_city"] = $row["name_city"];
		
		//lay khu vuc lay tu database	
$result_ds = mysql_query("SELECT * FROM district where id_city=".$row["cid"]) or die(mysql_error());

		// check for empty result
		if (mysql_num_rows($result_ds) > 0) {
			// looping through all results
			// products node
			while ($row = mysql_fetch_array($result_ds)) {
				// temp user array
				$district_t = array();
				$district_t["did"] = $row["did"];
				$district_t["name_district"] = $row["name_district"];
				$district_t["id_city"] = $row["id_city"];
				// push single product into final response array
				$district[$city["name_city"]][] = $district_t;

			}
			//$district[$city["name_city"]]['success'] = 1;
		}
		// $city[khu_vuc] = build khu vuc moi lay ra
        // push single product into final response array
        array_push($response["citys"], $city);
    }
    // success
    $response["success"] = 1;
    // echoing JSON response
	echo json_encode($district);exit;
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No citys found";
    // echo no users JSON
    echo json_encode($response);
}
?>
